package org.yi.web.category;

import java.util.Calendar;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yi.web.base.BaseController;
import org.yi.web.category.entity.CategoryEntity;

public class CategoryController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(CategoryController.class);
	
	/**
	 * add new category
	 */
	public void add(){
		setAttr("cats", CategoryEntity.dao.find("select * from f_category where pid is null or pid = ''"));
		render("add.html");
	}
	
	/**
	 * list all categories
	 */
	public void index(){
		
		render("index.html");
	}
	
	public void save(){
		try{
			new CategoryEntity()
				.set("name", getPara("name"))
				.set("alias", getPara("alias"))
				.set("pid", StringUtils.isBlank(getPara("pid")) ? null : getPara("pid"))
				.set("url", getPara("url"))
				.set("create_time", Calendar.getInstance().getTime())
				.set("create_user", getCurrentUser().getLong("id"))
				.save();
		}catch(Exception e){
			logger.error("sava category error: ", e);
			addError(e.getMessage());
		}
		redirect("/admin/category");
	}
	
}
